package com.example.node.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.node.R;
import com.example.node.adapter.MovieListAdapter;
import com.example.node.bean.MovieListBean;
import com.example.node.entity.Constant;
import com.example.node.listener.OnClick;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.MovieListModel;
import com.example.node.ui.MovieMoreActivity;
import com.example.node.utils.LogUtils;
import com.example.node.utils.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MovieFragment extends Fragment {

    private RecyclerView mMovieRecyclerView;
    private List<MovieListBean> mList=new ArrayList<>();
    private MovieListAdapter movieListAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mMovieRecyclerView = view.findViewById(R.id.mMovieRecyclerView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        mMovieRecyclerView.setLayoutManager(linearLayoutManager);
        movieListAdapter=new MovieListAdapter(mList);
        mMovieRecyclerView.setAdapter(movieListAdapter);
        initMovieData();
        movieListAdapter.setOnClick(new OnClick() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(getActivity(), MovieMoreActivity.class);
                MovieListBean movieListBean= mList.get(position);
                intent.putExtra("title",movieListBean.getTitle());
                intent.putExtra("id",movieListBean.getId());
                startActivity(intent);
            }
        });
    }

    //请求电影列表数据
    private void initMovieData() {
        OkHttpUtils.get(Constant.MOVIE_LIST, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {
                LogUtils.I(e.toString());
            }

            @Override
            public void onFailure(String meg) {
                LogUtils.I(meg);
            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    loadMovieData(json);
                } else {
                    LogUtils.I("数据为空");
                }
            }
        });
    }

    //加载电影数据
    private void loadMovieData(String json) {
        MovieListModel movieListModel = new Gson().fromJson(json, MovieListModel.class);
        for (int i=0;i<movieListModel.getData().size();i++){
            String title=movieListModel.getData().get(i).getTitle();
            String subtitle=movieListModel.getData().get(i).getSubtitle();
            String img_url=movieListModel.getData().get(i).getImg_url();
            String id=movieListModel.getData().get(i).getItem_id();
            MovieListBean movieListBean=new MovieListBean();
            movieListBean.setTitle(title);
            movieListBean.setSubtitle(subtitle);
            movieListBean.setImg_url(img_url);
            movieListBean.setId(id);
            mList.add(movieListBean);
            movieListAdapter.notifyDataSetChanged();
        }
    }
}
